package dataStructure.Search;

public class BinarySearch {
    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = left + (right - left) / 2;

        while (target != nums[mid]) {
            if (target < nums[mid]) {
                right = mid - 1;
            } else if (target > nums[mid]) {
                left = mid + 1;
            }
            if (left > right) return -1;
            mid = left + (right - left) / 2;
        }
        return mid;
    }
}
